package com.buba.controller.shuju;

import com.alibaba.fastjson.JSONObject;
import com.buba.pojo.Data;
import com.buba.pojo.Dates;
import com.buba.pojo.User;
import com.buba.service.shuju.ShujuService;
import com.buba.utils.MyContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("shuju")
@Controller
public class ShujuController {
    @Autowired
    @Qualifier("shujuService")
    ShujuService shujuService;

    @RequestMapping("toDataList")
    public String toDataList(){
        return "/shuju/dataList";
    }
    @RequestMapping("listData")
    @ResponseBody
    public Map<String, Object> listData(@RequestBody Dates dates,HttpSession session) throws ParseException {
        Map<String,Object> m=new HashMap<>(16);
//        //取出json对象的属性
//        JSONObject jsonObject=JSONObject.parseObject(jsonStr);
//        String beginStr=jsonObject.getString("begin");
//        String endStr=jsonObject.getString("end");
//        //string转换成Date类型
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        Date begin = sdf.parse(beginStr);
//        Date end= sdf.parse(endStr);
        //为Dates对象赋值
//        Dates dates=new Dates();
//        dates.setBegin(begin);
//        dates.setEnd(end);
        User user=(User)session.getAttribute(MyContent.DATA);
        //根据日期模糊查询
        List<Data> list=shujuService.listData(user.getRole(),dates);
        m.put(MyContent.CODE,"0000");
        m.put(MyContent.MSG,"查询成功");
        m.put("role",user.getRole());
        m.put(MyContent.DATA,list);
        return m;

    }
}
